package com.shilei.singleton;

/**
 * @description: 懒汉式
 *      lazy loading
 *      虽然达到按需初始化的目的， 但却带来线程不安全问题
 *
 *      可以通过synchronized , 但也带来效率的问题
 * @author: 81271
 * @date: 2020-04-12 10:02
 */
public class Mgr02 {
    private static Mgr02 INSTANCE;

    private Mgr02() {
    }

    public static Mgr02 getInstance() {
        if (INSTANCE == null) {
            try {
                Thread.sleep(1);
                INSTANCE = new Mgr02();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> System.out.println(Mgr02.getInstance().hashCode())).start();
        }
    }
}
